//
//  CZNavViewController.m
//  实验
//
//  Created by xhd on 16/5/5.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "AYMRegisterViewController.h"
#import "AYMLoginViewController.h"
#define UIHeight 30
@interface AYMRegisterViewController ()
@property (nonatomic, weak) UITextField *telField;
@property (nonatomic, weak) UITextField *VerificationCodeField;
@property (nonatomic, weak) UITextField *invitationText;
@property (nonatomic, weak) UIButton *seletedButton;
@end

@implementation AYMRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self setLeftImage:self.telField leftImageNamed:@"product_img_icon_xieyi.png"];
    [self setLeftImage:self.VerificationCodeField leftImageNamed:@"product_img_icon_xieyi.png"];
    [self setLeftImage:self.invitationText leftImageNamed:@"product_img_icon_xieyi.png"];
    
    
    
}


-(void)setupUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"用户注册";
    //手机号输入框
    UITextField *telField = [[UITextField alloc]init];
    telField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    telField.layer.borderWidth = 1;
    telField.layer.cornerRadius = 4;
    telField.font = [UIFont systemFontOfSize:14];
    
    //设置键盘为九宫格样式
    telField.keyboardType = UIKeyboardTypeNumberPad;
    telField.placeholder = @"输入手机号";
    
    self.telField = telField;
    
    [self.view addSubview:telField];
    //验证码输入框
    UITextField *VerificationCodeField = [[UITextField alloc]init];
    [self.view addSubview:VerificationCodeField];
    VerificationCodeField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    VerificationCodeField.layer.cornerRadius = 4;
    VerificationCodeField.placeholder = @"输入验证码";
    VerificationCodeField.font = [UIFont systemFontOfSize:14];
    VerificationCodeField.keyboardType = UIKeyboardTypeNumberPad;
    VerificationCodeField.layer.borderWidth = 1;
    self.VerificationCodeField = VerificationCodeField;
    
    
    //获取验证码按钮
    UIButton *getVerification = [[UIButton alloc]init];
    [self.view addSubview:getVerification];
    [getVerification setBackgroundImage:[UIImage imageNamed:@"e5e5e5"] forState:(UIControlStateNormal)];
    
    [getVerification setTitleColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"nav"]] forState:UIControlStateNormal];
    getVerification.titleLabel.font = [UIFont systemFontOfSize:14];
    [getVerification setTitle:@"获取验证码" forState:(UIControlStateNormal)];
    
    
    
    // 监控 获取验证码按钮
    [getVerification addTarget:self action:@selector(didClickGetVerrifiation) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    UIButton * agreementButton = [UIButton buttonWithType:UIButtonTypeSystem];
    agreementButton.titleLabel.font = [UIFont systemFontOfSize:13];
    agreementButton.layer.cornerRadius = 3;
    [agreementButton setTitleColor:[UIColor colorWithRed:91.0/255 green:160.0/255 blue:232.0/255 alpha:1] forState:UIControlStateNormal];
    
    [agreementButton setTitle:@"用户协议" forState:(UIControlStateNormal)];
    
    [self.view addSubview:agreementButton];
    
    // 点击阅读协议
    [agreementButton addTarget:self action:@selector(didClickAgreenmentButton) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIButton *nextStep = [[UIButton alloc]init];
    [nextStep setTitleColor:[UIColor colorWithWhite:1 alpha:0.2] forState:UIControlStateHighlighted];
    [nextStep setTitle:@"下一步" forState:(UIControlStateNormal)];

    nextStep.backgroundColor = [UIColor colorWithRed:41/255.0 green:188/255.0 blue:186/255.0 alpha:1];
    
    [self.view addSubview:nextStep];
    
    [nextStep addTarget:self action:@selector(didClickNextStep) forControlEvents:(UIControlEventTouchUpInside)];
    
    //邀请码输入框
    UITextField *invitationText = [[UITextField alloc]init];
    invitationText.placeholder = @"请输入邀请码(选填)";
    invitationText.layer.borderColor = [UIColor lightGrayColor].CGColor;
    invitationText.font = [UIFont systemFontOfSize:14];
    invitationText.keyboardType = UIKeyboardTypeNumberPad;
    invitationText.layer.cornerRadius = 4;
    invitationText.layer.borderWidth = 1;
    [self.view addSubview:invitationText];
    self.invitationText = invitationText;
    //添加点击复选框
    UIButton *seletedButton = [[UIButton alloc]init];
    [seletedButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [seletedButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    
    seletedButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [seletedButton setTitle:@" 本人已同意并阅读" forState:(UIControlStateNormal)];
    [seletedButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [seletedButton addTarget:self action:@selector(clickSeletedButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.seletedButton = seletedButton;
    [self.view addSubview:seletedButton];
    
    //直接登陆
    UIButton *directLogin = [[UIButton alloc]init];
    [directLogin setTitle:@"已有账号,直接登陆" forState:(UIControlStateNormal)];
    [directLogin setTitleColor:[UIColor colorWithRed:91.0/255 green:160.0/255 blue:232.0/225 alpha:1]forState:(UIControlStateNormal)];
    
    
    [directLogin addTarget:self action:@selector(didClickDirectLogin) forControlEvents:(UIControlEventTouchUpInside)];
    
    directLogin.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [self.view addSubview:directLogin];
    
    //布局控件
    
    [directLogin makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(nextStep.bottom).offset(130);
        make.centerX.equalTo(self.view.centerX);
        make.width.equalTo(150);
        make.height.equalTo(20);
    }];
    [invitationText makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(getVerification.bottom).offset(20);
        make.leading.trailing.equalTo(telField);
        make.height.equalTo(UIHeight);
    }];
    
    
    [telField makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(homeMargin);
        make.trailing.equalTo(self.view).offset(-homeMargin);
        make.top.equalTo(self.view).offset(80);
        make.height.equalTo(UIHeight);
    }];
    
    [getVerification makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(telField.trailing);
        make.width.equalTo(100);
        make.top.equalTo(telField.bottom).offset(25);
        make.height.equalTo(UIHeight);
        
        
    }];
    
    [VerificationCodeField makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(telField);
        make.trailing.equalTo(getVerification.leading).offset(-20);
        make.top.equalTo(telField.bottom).offset(25);
        make.height.equalTo(UIHeight);
    }];
    
    
    [agreementButton makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(seletedButton.trailing);
        make.top.equalTo(seletedButton.top).offset(15);
        make.width.equalTo(70);
        make.height.equalTo(20);
        
    }];
    
    [nextStep makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(seletedButton.bottom).offset(60);
        make.leading.trailing.equalTo(telField);
        make.height.equalTo(UIHeight);
        
    }];
    
    [seletedButton makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(invitationText.leading).offset(50);
        make.top.equalTo(invitationText.bottom).offset(20);
        make.width.equalTo(130);
        make.height.equalTo(50);
    }];
    
}

-(void)setLeftImage:(UITextField *)textField leftImageNamed:(NSString *)imageName
{
    // 初始化imageView
    UIImageView *leftView = [[UIImageView alloc] init];
    leftView.image = [UIImage imageNamed:imageName];
    leftView.frame = CGRectMake(0, 0, 30, 30);
    
    // 设置内容居中
    leftView.contentMode = UIViewContentModeCenter;
    textField.leftView = leftView;
    
    // 设置左边的image永远显示
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    // 设置右边永远显示 X
    textField.clearButtonMode = UITextFieldViewModeAlways;
    
    
}
// 登陆
-(void)didClickDirectLogin{
    Log(@"有账号直接登陆");
    
    //创建Login故事板
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    //创建Login控制器
    AYMLoginViewController *loginVC = [board instantiateViewControllerWithIdentifier:@"AYMLogin"];
    
    [self.navigationController pushViewController:loginVC animated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

//点击下一步的实现
-(void)didClickNextStep{
    Log(@"点击了");
    if (self.telField.text.length == 0 || self.VerificationCodeField.text.length == 0 ) {
        
    }
    if (!self.seletedButton.selected) {
        [SVProgressHUD showErrorWithStatus:@"同意用户协议"];
    }
    
}




- (void)clickSeletedButton:(UIButton *)button{
    button.selected = !button.selected;
}


-(void)didClickGetVerrifiation{
    
    if (self.telField.text.length != 11) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确手机号"];
    }else {
        //获取验证码
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.telField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
            
            if (error) {
                
                [SVPAlertManager alertErrorStatus:@"发送验证码失败"];
                
            }else{
                
                Log(@"成功");
            }
            
        }];
    }
}


-(void)didClickAgreenmentButton{
    
    if (self.seletedButton.selected) {
        printf("开始阅读协议");
    }else{
        [SVPAlertManager alertErrorStatus:@"同意阅读"];
    }
}

@end
